package com.ktm.gecco.crawler.boche.pipeline;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.ktm.gecco.crawler.boche.bean.BocheCarDetail;
import com.ktm.gecco.crawler.boche.entity.BocheCar;
import com.ktm.gecco.crawler.boche.service.BocheCarService;
import com.ktm.gecco.crawler.main.service.base.BaseUserLog;

@Service
public class BocheCarDetailPipeline extends BaseUserLog implements Pipeline<BocheCarDetail> {
    @Autowired
    private BocheCarService bocheCarService;

    public static long count =0;

    private int page=0;

    @SuppressWarnings("static-access")
    public void process(BocheCarDetail carDetail) {
        page++;
        List<String> list = new BocheCarIndexPipelines().urls;
        HttpRequest request=carDetail.getRequest();
        if(page<list.size()){

            //转换数据
            BocheCar car = new BocheCar(carDetail);
            BocheCar getCar = bocheCarService.getBocheCarByVin(car.getVin());
            if(car!=null){
                System.out.println("car.getAccidentType():"+car.getAccidentType());
                if(!car.getAccidentType().equals("二手") && !car.getAccidentType().equals("新车")){

                    if(getCar!=null){
                        System.out.println(car.getVin()+"这个车辆已经在数据库存在！");
                        this.saveUserLog("博车网",carDetail.getVin()+"已经在数据库存在");
                    }else{
                        this.saveUserLog("博车网","采集"+car.getCarname()+carDetail.getVin());
                        //保存数据到数据库
                        bocheCarService.batchSaveBoche(car);
                    }
                }

            }

            //进入下一个详情页
            String urlstr =list.get(page);
            DeriveSchedulerContext.into(request.subRequest(urlstr));
        }

    }


}
